#include <stdio.h>
#define CSIZE 4
#define N_SORCE 3

struct name {
    char * first_name;
    char * last_name;
};

struct student {
    struct name s_name;
    float grade [3];
    float avg;
};

// 获取学生成绩
void get_score(struct student info[], int n);
// 获取某个学生成绩的平均值
void get_aver(struct student info[], int n);
// 打印结构的信息
void print_info(struct student info[], int n);
// 获取班级平均分
void get_all_aver(struct student info[], int n);

int main(void)
{
    struct student students[CSIZE] = {
            {{"Paulo", "Costa"}},
            {{"Tony", "Ferguson"}},
            {"Dana", "White"},
            {"Conor", "McGregor"},
    };

    get_score(students, CSIZE);
    get_aver(students, CSIZE);
    print_info(students, CSIZE);
    get_all_aver(students, CSIZE);

    return 0;
}

void get_score(struct student info[], int n)
{
    int i, j;

    for (i = 0; i < n ; i++)
    {
        printf("请输入%s %s的三门学科的成绩: ", info[i].s_name.first_name, info[i].s_name.last_name);
        for (j = 0; j < N_SORCE; j++)
            scanf("%f", &info[i].grade[j]);
    }
}

void get_aver(struct student info[], int n)
{
    int i, j;
    float sum_score [CSIZE] = {0, 0, 0, 0};

    for (i = 0; i < CSIZE; i++)
        for (j = 0; j < N_SORCE; j++)
            sum_score[i] += info[i].grade[j];

    for (i = 0; i < CSIZE; i++)
    {
        info[i].avg = sum_score[i] / N_SORCE;
        printf("%s %s 的平均成绩为：%.3f \n", info[i].s_name.first_name, info->s_name.last_name, info[i].avg);
    }
    printf("\n");
}

void print_info(struct student info[], int n)
{
    int i, j;

    printf("    Name%15s    %s    %s    average\n", "score1", "score2", "score3");
    for (i = 0; i < CSIZE; i++)
    {
        printf("%5s%-10s ", info[i].s_name.first_name, info[i].s_name.last_name);
        for (j = 0; j < N_SORCE; j++)
            printf("%6gf ", info[i].grade[j]);
        printf("%11.2f\n", info[i].avg);
    }
    printf("\n");
}

void get_all_aver(struct student info[], int n)
{
    int i;
    float total = 0;

    for (i = 0; i < n; i++)
    {
        total += info[i].avg;
    }
    printf("班级平均分是 %.2f.\n", total / CSIZE);
}